package com.kx.yooli.kx.wechat.utils.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
import java.util.regex.Pattern;

import static javax.management.Query.match;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/8
 * PROJECT_NAME wechat
 * description create MD5Util by john_sqwen
 * and  value is
 */
public class MD5Util {
    private final static Logger log = LoggerFactory.getLogger(MD5Util.class);

    /**
     * @Description: 根据openId获取客户身份证信息,加密盐
     */
    public static final String SLAT = "5215f238-2808-4525-a4de-d38c9b752911";

    private static final String DEFAULT_CHARSET = "utf-8";
    private static final String VI = "w&Y@5pM3";

    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        try {
            byte[] btInput = s.getBytes("utf-8");
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * md5 加密
     * @param message 字符串
     * @param salt 加密盐
     * @param isVI 向量
     * @return String builder.toString()
     */
    public static String md5(String message, String salt){
        StringBuilder builder = new StringBuilder(32);
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            digest.reset();
            digest.update(message.getBytes(DEFAULT_CHARSET));
            digest.update(salt.getBytes(DEFAULT_CHARSET));
            byte[] b = digest.digest();
            for (byte c : b) {
                builder.append(String.format("%02x", c));
            }
        } catch (Exception e) {
            log.error("md5 加密失败", e);
        }
        return builder.toString();
    }

    public static boolean checkMailStyle(String str){
        String vaildEmaill = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        return Pattern.compile(vaildEmaill).matcher(str).matches();
    }



    public static void main(String[] args) {
        System.out.println(MD5Util.MD5("您本次操作的验证码为：123456，5分钟内有效"));
        System.out.println(MD5Util.MD5("加密"));


    }
}
